const hprose = require('hprose');

const proxyFunc = (url) => {
  return new Promise((resolve, reject) => {
    try {
      const client = hprose.Client.create(url);
      client.ready(proxy => {
        resolve(proxy);
      });
    } catch (e) {
      reject(e);
    }
  });
};

think.beforeStartServer(async() => {
  const rpcConfig = think.config('rpc');
  think.rpc = {};
  Object.keys(rpcConfig).forEach(async(name) => {
    const url = rpcConfig[name];
    think.rpc[name] = await proxyFunc(url);
    think.logger.info('rpc client ' + name + ' created');
  });
});
